package org.netspeak.android.app;

import java.util.List;

import org.netspeak.android.utils.ParcelablePhrase;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * Main Activity, showing Netspeak search interface and result list.
 * 
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class NetspeakActivity extends Activity {
    
    /** Tag for LogCat logging */
    private static final String TAG = "NetspeakActivity";
    
    /** Key for data exchange with Intents, identifying phrases */
    public static final String PHRASES = "phrases";
    
    /** Key for data exchange with Intents, identifying total frequency */
    public static final String FREQUENCY = "frequency";
    
    /** Key for data exchange with Intents, identifying query */
    public static final String QUERY = "query";
    
    private EditText mInput;
    private LinearLayout mExampleLayout;
    private ListView mResultList;
    private ResultListArrayAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mInput = (EditText) findViewById(R.id.input);
        mExampleLayout = (LinearLayout) findViewById(R.id.examples);
        View child = getLayoutInflater().inflate(R.layout.examples, null);
        mExampleLayout.addView(child);
        mResultList = (ListView) findViewById(R.id.resultList);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            List<ParcelablePhrase> phrases = extras
                    .getParcelableArrayList(PHRASES);
            long totalFrequency = extras.getLong(FREQUENCY);
            String query = extras.getString(QUERY);
            if (phrases != null) {
                // hide examples
                mExampleLayout.setVisibility(View.GONE);
                // put current query string as text into input field
                mInput.setText(query);
                // create an set adapter to list view for showing results
                mAdapter = new ResultListArrayAdapter(NetspeakActivity.this, 
                        R.layout.result, phrases, totalFrequency);
                mResultList.setAdapter(mAdapter);
            }
        }        
    }
    
    /**
     * Method executed at onClick event of search button. Takes current value 
     * of {@link NetspeakActivity#mInput} and executes a 
     * {@link NetspeakRequestTask} with it.
     * 
     * @param view  the view that calls this method
     */
    public void doNetspeakRequest(View view) {
        String inputString = mInput.getText().toString();
        new NetspeakRequestTask(this).execute(inputString);
    }
    
    /** 
     * Method executed at onClick event of example text. Executes a new 
     * {@link NetspeakRequestTask} with example text, according to which 
     * example view called the method.
     * 
     * @param view  the view that calls this method
     */
    public void doExampleNetspeakRequest(View view) {
        switch (view.getId()) {
        case R.id.example1:
            String example1 = getResources().getString(R.string.example1);
            mInput.setText(example1);
            new NetspeakRequestTask(this).execute(example1);
            break;
        case R.id.example2:
            String example2 = getResources().getString(R.string.example2);
            mInput.setText(example2);
            new NetspeakRequestTask(this).execute(example2);
            break;
        case R.id.example3:
            String example3 = getResources().getString(R.string.example3);
            mInput.setText(example3);
            new NetspeakRequestTask(this).execute(example3);
            break;
        case R.id.example4:
            String example4 = getResources().getString(R.string.example4);
            mInput.setText(example4);
            new NetspeakRequestTask(this).execute(example4);
            break;
        case R.id.example5:
            String example5 = getResources().getString(R.string.example5);
            mInput.setText(example5);
            new NetspeakRequestTask(this).execute(example5);
            break;
        default:
            Log.v(TAG,
                    "doExampleNetspeakRequest has been called from " +
                    "unexpected view");
        }
    }

}